package org.ouwh.fw.util.xml;

import java.util.HashMap;

/**
 * 
 */
public final class Attribute {
	private String name;
	private String value;
	private Object obj;

	public Attribute(String name, String value) {
		this.name = name;
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public String getValue() {
		return value;
	}

	public Attribute setValue(String value) {
		this.value = value;
		return this;
	}

	public boolean equals(Attribute attr) {
		return attr.getName().equals(name) && attr.getValue().equals(value);
	}

	public static String getValue(Attribute[] attrs, String name) {
		for (int i = 0; i < attrs.length; i++) {
			if (attrs[i].getName().equals(name))
				return attrs[i].getValue();
		}
		return null;
	}

	public static Attribute getAttr(Attribute[] attrs, String name) {
		for (int i = 0; i < attrs.length; i++) {
			if (attrs[i].getName().equals(name))
				return attrs[i];
		}
		return null;
	}

	public static boolean include(Attribute[] src, Attribute[] target) {
		if (target == null)
			return true;
		if (src == null)
			return false;
		HashMap<String, Object> temp = new HashMap<>();
		for (int i = 0; i < src.length; i++)
			temp.put(src[i].getName(), src[i].getValue());
		for (int i = 0; i < target.length; i++) {
			String name = target[i].getName();
			String value = target[i].getValue();
			String att = (String) temp.get(name);
			if (att == null)
				return false;
			if (!att.equals(value))
				return false;
		}
		return true;
	}

	public static boolean include2(Attribute[] src, Attribute target) {
		if (target == null)
			return true;
		if (src == null)
			return false;
		for (int i = 0; i < src.length; i++) {
			if (src[i].equals(target))
				return true;
		}
		return false;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}
}
